package com.ruijie.acl.web;

import java.util.List;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.ruijie.acl.AccessGroup;
import com.ruijie.acl.AclDevice;
import com.ruijie.acl.AclInterface;
import com.ruijie.acl.TimeRange;
import com.ruijie.acl.service.AccessGroupService;
import com.ruijie.acl.service.AceService;
import com.ruijie.acl.service.AclInterfaceService;
import com.ruijie.acl.service.DeviceAccessGroupService;
import com.ruijie.acl.service.DeviceTimeRangeService;
import com.ruijie.acl.service.TimeRangeService;
import com.ruijie.acl.task.config.TaskConfigAclDevice;
import com.ruijie.acl.task.model.AccessGroupInfo;
import com.ruijie.acl.task.model.AclInterfaceInfo;
import com.ruijie.acl.task.model.TimeRangeInfo;
import com.ruijie.acl.utils.AclConfigUtil;
import com.ruijie.acl.utils.AclUtil;
import com.ruijie.batch.plan.PlanParameters;
import com.ruijie.foundation.bo.EntityObject;
import com.ruijie.foundation.dao.support.Page;
import com.ruijie.foundation.exception.BusinessException;
import com.ruijie.foundation.service.Repository;

/**
 * <p>
 * Title: ACL组管理Action类
 * 
 * 
 * 
 * 
 * 
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * 为ACL组管理模块提供页面数据的显示、获取和页面的跳转。
 * 
 * 
 * 
 * 
 * 
 * </p>
 * <p>
 * Copyright: Copyright (c) 2008
 * </p>
 * <p>
 * Company: Ruijie Co., Ltd.
 * </p>
 * <p>
 * Create Time:
 * </p>
 * <p>
 * </p>
 * <p>
 * Update Time:
 * </p>
 * <p>
 * Updater:
 * </p>
 * <p>
 * Update Comments:
 * </p>
 * 
 * @author ASAI
 */
@Name("deployAccessGroupAction")
@Scope(ScopeType.CONVERSATION)
public class DeployAccessGroupAction extends AclPlanBaseAction {

	@In("#{accessGroupService}")
	private AccessGroupService accessGroupService;

	@In("#{aceService}")
	private AceService aceService;

	@In("#{timeRangeService}")
	private TimeRangeService timeRangeService;

	@In("#{aclInterfaceService}")
	private AclInterfaceService aclInterfaceService;

	@In("#{taskConfigAclDevice}")
	private TaskConfigAclDevice taskConfigAclDevice;

	@In("#{deviceAccessGroupService}")
	private DeviceAccessGroupService deviceAccessGroupService;

	@In("#{deviceTimeRangeService}")
	private DeviceTimeRangeService deviceTimeRangeService;

	private AccessGroup criteria = new AccessGroup();

	private AccessGroup model;

	private AclDevice aclDevice;

	/**
	 * 取得设备分页信息
	 * 
	 * @return
	 */
	protected Page pagedQuery(EntityObject criteria, int page, int pageSize) {
		AccessGroup groupCriteria = (AccessGroup) criteria;
		groupCriteria.setIsChanged(null);
		return accessGroupService.pageQuery(groupCriteria, page, pageSize);
	}

	@Override
	@SuppressWarnings("unchecked")
	protected Repository getRepository() {
		return accessGroupService;
	}

	@Override
	protected String getAddPage() {
		return null;
	}

	@Override
	public EntityObject getCriteria() {
		return criteria;
	}

	public String toDeploy(AclDevice aclDevice) {
		this.criteria = new AccessGroup();
		this.aclDevice = aclDevice;
		list();
		return "/acl/deployAccessGroup.seam";
	}

	@Override
	protected String getDetailPage() {
		return "/acl/aclDeviceDetail.seam";
	}

	@Override
	protected String getListPage() {
		return "/acl/aclDeviceDetail.seam";
	}

	@Override
	public EntityObject getModel() {
		model.setDeleteFlg(false);
		return model;
	}

	@Override
	protected String getUpdatePage() {
		return null;
	}

	@Override
	public void resetModel() {
		model = null;
		model = new AccessGroup();
		this.model.setIsChanged(false);
	}

	@Override
	public void setCriteria(EntityObject criteria) {
		this.criteria = (AccessGroup) criteria;
	}

	@Override
	public void setModel(EntityObject model) {
		this.model = (AccessGroup) model;
	}

	public void resetNewestModel() {
		try {
			AccessGroup newestModel = accessGroupService
					.get(this.model.getId());
			this.setModel(newestModel);
		} catch (Exception ex) {
			//
		}
	}

	/**
	 * 部署所有选择的组
	 */
	public void deployAccessGroup() {
		if (null == getIds() || getIds().length == 0) {
			return;
		}
		try {
			String idString = "";
			for (Long id : getIds()) {
				idString += "," + id.toString();
			}
			if (!AclUtil.empty(idString)) {
				PlanParameters planParameters = new PlanParameters();
				idString = idString.substring(1);
				List<AccessGroup> accessGroupList = accessGroupService
						.findByIds(idString);
				// 转换组信息
				if (accessGroupList.size() > 0) {
					List<AccessGroupInfo> accessGroupInfoList = aceService
							.accessGroup2AccessGroupInfo(accessGroupList);
					planParameters.putParameter("accessGroupInfoList",
							accessGroupInfoList);
				}

				// 转换时间段信息
				List<TimeRange> timeRangeList = null;
				if (accessGroupList != null && accessGroupList.size() > 0) {
					timeRangeList = timeRangeService
							.findByAccessGroup(accessGroupList);
				}

				if (timeRangeList != null && timeRangeList.size() > 0) {
					List<TimeRangeInfo> timeRangeInfoList = AclConfigUtil
							.timeRangeList2TimeRangeInfoList(timeRangeList);
					planParameters.putParameter("timerangeInfoList",
							timeRangeInfoList);
				}

				List<AclInterface> interfaceList = aclInterfaceService
						.findByAccessGroup(accessGroupList);

				if (interfaceList != null && interfaceList.size() > 0) {
					for (int i = 0; i < interfaceList.size(); i++) {
						AclInterface aclInterface = interfaceList.get(i);
						if (!aclInterface.getDevice().getId().equals(
								this.aclDevice.getDevice().getId())) {
							interfaceList.remove(i);
							i--;
						}
					}
					if (interfaceList != null && interfaceList.size() > 0) {
						List<AclInterfaceInfo> aclInterfaceInfoList = AclConfigUtil
								.aclInterface2AclInterfaceInfoList(interfaceList);
						planParameters.putParameter("aclInterfaceInfoList",
								aclInterfaceInfoList);
					}
				}

				taskConfigAclDevice.processItem(this.aclDevice.getDevice(),
						planParameters);

				// 不用处理时间段与组之间的关系。
				// 插入组和设备之间的关系。
				deviceAccessGroupService.addAccessGroupDevices(accessGroupList,
						this.aclDevice.getDevice());
				// 备份设备中的组
				this.accessGroupService.saveSameWithDevice(accessGroupList,
						this.aclDevice.getDevice());
				// 变更组的变更警示
				if (accessGroupList != null && accessGroupList.size() > 0) {
					for (AccessGroup accessGroup : accessGroupList) {
						try {
							accessGroupService.refresh(accessGroup);
							accessGroup.setIsChanged(false);
							accessGroupService.update(accessGroup);
						} catch (Exception ex) {
							//
						}
					}
				}

				// 插入时间段和设备的关系。
				deviceTimeRangeService.addTimeRangeDevices(timeRangeList,
						this.aclDevice.getDevice());
				// 备份设备中的时间段
				this.timeRangeService.saveSameWithDevice(timeRangeList,
						this.aclDevice.getDevice());

				// 变更时间段的变更警示
				if (timeRangeList != null && timeRangeList.size() > 0) {
					for (TimeRange timeRange : timeRangeList) {
						try {
							timeRangeService.refresh(timeRange);
							timeRange.setIsChanged(false);
							timeRangeService.update(timeRange);
						} catch (Exception ex) {
							//
						}
					}
				}

			}
		} catch (BusinessException e) {
			// 清除多选框选定记录
			selectedIds.clear();
			throw e;
		} catch (Exception e) {
			selectedIds.clear();
			throw new BusinessException(e.getMessage());
		}
		updateModel();
		// 如果当前页面已经没有记录(例如删除了当前页面的所有记录)，那么显示上一页

		if (currentPage.getTotalPageCount() < page && page > 1) {
			page--;
			updateModel();
		}
		// 清除多选框选定记录
		selectedIds.clear();
		
		addGlobalI18nMessage("acl_device_deploy_success");
		
		redirectTo(this.getListPage());
	}

}
